package cn.zhoujing.demo.redis.controller;

import cn.zhoujing.demo.redis.common.CommonResult;
import cn.zhoujing.demo.redis.common.dto.LoginDTO;
import cn.zhoujing.demo.redis.common.dto.LogoutDTO;
import cn.zhoujing.demo.redis.service.LoginService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 登录服务
 *
 * @author zhoujing
 * @createTime 2023/12/1 - 18:00
 */
@RestController
@RequestMapping(value = "/auth")
public class LoginController {

    @Resource
    private LoginService loginService;


    @RequestMapping(value = "/token", method = RequestMethod.POST)
    public CommonResult<OAuth2AccessToken> postAccessToken(@RequestBody @Validated LoginDTO dto) throws HttpRequestMethodNotSupportedException, JsonProcessingException {
        OAuth2AccessToken  accessToken = loginService.postAccessToken(dto);
        return CommonResult.success(accessToken);
    }

    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public CommonResult<Boolean> removeAccessToken(@RequestBody @Validated LogoutDTO dto){
        boolean removed = loginService.removeAccessToken(dto);
        return CommonResult.success(removed);
    }


}
